import * as vscode from 'vscode';
import { _global } from '../util/CONST';
import * as util from '../util/util';
import { headerAnnotation } from './createAnnotation';
import { watchSaveFn } from './fileSave';

export class ActiveHandle {
  watch() {
    watchSaveFn() // 监听文件保存
    this.createFile()
    this.errPath()
    this.watchTable()
  }

  watchTable() {
    const table = vscode.commands.registerCommand(
      'koroFileheader.table',
      () => {
        const editor = vscode.window.activeTextEditor // 每次运行选中文件
        const activeLine = editor.selection.active.line // 激活行 行号
        const position = editor.selection.active
        const newPosition = position.with(activeLine + 1, 10000)
        editor.selection = new vscode.Selection(newPosition, newPosition)
      })
    _global.context.subscriptions.push(table)
  }

  // 监听用户输入的错误日志地址
  errPath() {
    const errPath = vscode.commands.registerCommand(
      'fileheader.errPathSet',
      () => {
        vscode.window
          .showOpenDialog({
            canSelectFiles: false, // 允许选择文件
            canSelectFolders: true, // 是否可以选择文件夹
            canSelectMany: false // 是否可以选择多个文件
          })
          .then((urlArr) => {
            try {
              if (!urlArr) return // 用户取消选择
              vscode.window.showInformationMessage(
                'fileHeader: 设置错误日志地址成功'
              )
              _global.errPath = urlArr[0].path
              // 储存到空间
              _global.context.globalState.update(_global.globalStateData.errPath, _global.errPath)
            } catch (err) {
              // handleError.showErrorMessage('fileHeader: 设置错误日志地址', err)
            }
          })
      }
    )
    _global.context.subscriptions.push(errPath)
  }

  // 创建文件 自动添加注释
  createFile() {
    console.log("监听创建文件自动添加注释");
    vscode.workspace.onDidCreateFiles((file) => {
      console.log("onDidCreateFiles", file);
      const config = vscode.workspace.getConfiguration('fileheader') // 配置项默认值
      if (!config.configObj.createHeader) return // 关闭
      const filePath = file.files[0].fsPath
      const openPath = vscode.Uri.file(filePath)
      console.log("openPath", openPath);
      vscode.workspace.openTextDocument(openPath).then((doc) => {
        console.log("openTextDocument", doc);
        vscode.window.showTextDocument(doc).then(() => {
          console.log("showTextDocument", doc);
          try {
            const editor = vscode.window.activeTextEditor // 每次运行选中文件
            const fsPath = editor.document.uri.fsPath
            const hasAddProhibit = util.authList(fsPath)
            if (!hasAddProhibit) return false // 被添加进黑名单 或者没有添加进白名单
            headerAnnotation(editor, {
              create: true
            })
          } catch (err) {
            // handleError.showErrorMessage('fileHeader: 自动添加注释', err)
          }
        })
      })
    })
  }
}
